package com.system.borrow.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.system.borrow.Constant.URLDefind;
import com.system.borrow.R;
import com.system.borrow.cache.CacheMode;
import com.system.borrow.callback.DialogCallback;
import com.system.borrow.http.model.HttpParams;
import com.system.borrow.http.utils.OkHttpUtils;
import com.system.borrow.utils.SecondTitle;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class ForgetPwdActivity extends BaseActivity implements View.OnClickListener {

    private EditText et_password;
    private Button btn_save;
    private String mobile,code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pwd);
        SecondTitle title = new SecondTitle(this);
        title.setTitle("忘记密码",null);
        mobile = (String) getIntent().getSerializableExtra("mobile");
        code = (String) getIntent().getSerializableExtra("code");
        initView();
        initListener();
    }

    private void initListener() {
        btn_save.setOnClickListener(this);
    }

    private void initView() {
        et_password = (EditText)findViewById(R.id.et_password);
        btn_save = (Button)findViewById(R.id.btn_save);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_save:
                String password = et_password.getText().toString().trim();
                if(password.isEmpty()){
                    ToastShow("请输入密码");
                }else{
                    reset(password);
                }
                break ;
        }
    }

    private void reset(String password) {
        HttpParams parmas = new HttpParams();
        parmas.put("mobile",mobile);
        parmas.put("code",code);
        parmas.put("password",password);
        OkHttpUtils.post(URLDefind.RESET)
                .tag(this)
                .cacheMode(CacheMode.NO_CACHE)
                .params(parmas)
                .execute(new DialogCallback<String>(this,String.class,null) {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            int statusCode = jsonObject.getInt("statusCode");
                            if (statusCode == 200) {
                                ToastShow("密码重置成功,请登录");
                                finish();
                            } else {
                                ToastShow(jsonObject.getString("message"));
                            }
                        }catch (JSONException e){
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onCacheResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {

                    }

                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                });
    }
}
